using System.Security.Principal;
using System.Web;
using System.Web.Security;

namespace System.Utility.Security
{
	public class WebSecurityService
	{
		public static string DomainUrl;

		public static int ExpireMinutes;

		public static bool IsPersistent;

		public static bool IsAuthenticated
		{
			get
			{
				return ((Principal == null ? true : Principal.Identity == null) ? false : Principal.Identity.IsAuthenticated);
			}
		}

		public static Principal Principal
		{
			get
			{
				Principal principal;
				IPrincipal user = HttpContext.Current.User;
				if (user == null)
				{
					principal = null;
				}
				else
				{
					principal = user as Principal;
				}
				return principal;
			}
		}

		public static int RoleId
		{
			get
			{
				return UserInfo.RoleId;
			}
		}

		public static string UserId
		{
			get
			{
				return UserInfo.UserId;
			}
		}

		public static UserInfo UserInfo
		{
			get
			{
                UserInfo userInfo;
				userInfo = (Principal == null ? new UserInfo() : Principal.UserInfo);
				return userInfo;
			}
		}

		public static string UserName
		{
			get
			{
				return UserInfo.UserName;
			}
		}

		static WebSecurityService()
		{
			DomainUrl = string.Empty;
			ExpireMinutes = 1440;
			IsPersistent = false;
		}

		public WebSecurityService()
		{
		}

		public static void Clear(bool IsAddDomain)
		{
			HttpCookie httpCookie = new HttpCookie(FormsAuthentication.FormsCookieName, "");
			if (IsAddDomain)
			{
				httpCookie.Domain = DomainUrl;
			}
			httpCookie.Expires = DateTime.Now.AddHours(-1);
			HttpContext.Current.Response.Cookies.Add(httpCookie);
		}

		public static Principal LoadPrincipal()
		{
			Principal principal;
			HttpCookie item = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
			if (item != null)
			{
				FormsAuthenticationTicket formsAuthenticationTicket = FormsAuthentication.Decrypt(item.Value);
				if (formsAuthenticationTicket != null)
				{
					FormsIdentity formsIdentity = new FormsIdentity(formsAuthenticationTicket);
					Identity identity = new Identity()
					{
						Name = formsIdentity.Ticket.Name,
						IsAuthenticated = true
					};
					Principal userInfo = new Principal(identity);
					string userData = formsIdentity.Ticket.UserData;
					if (!string.IsNullOrEmpty(userData))
					{
						userInfo.UserInfo = new UserInfo(userData);
						HttpContext.Current.User = userInfo;
						principal = userInfo;
					}
					else
					{
						principal = null;
					}
				}
				else
				{
					principal = null;
				}
			}
			else
			{
				principal = null;
			}
			return principal;
		}

		public static void SignIn(string userId, string userName, int roleId = 0, bool IsAddDomain = false)
		{
			if ((string.IsNullOrEmpty(userId) ? false : !string.IsNullOrEmpty(userName)))
			{
				DateTime dateTime = DateTime.Now.AddMinutes((double)ExpireMinutes);
				string str = (new UserInfo(userId, userName, roleId)).ToString();
				FormsAuthenticationTicket formsAuthenticationTicket = new FormsAuthenticationTicket(1, userName, DateTime.Now, dateTime, IsPersistent, str, "/");
				string str1 = FormsAuthentication.Encrypt(formsAuthenticationTicket);
				HttpCookie httpCookie = new HttpCookie(FormsAuthentication.FormsCookieName, str1);
				if (formsAuthenticationTicket.IsPersistent)
				{
					httpCookie.Expires = dateTime;
				}
				if (IsAddDomain)
				{
					httpCookie.Domain = DomainUrl;
				}
				HttpContext.Current.Response.Cookies.Add(httpCookie);
			}
		}

		public static void SignOut()
		{
			FormsAuthentication.SignOut();
		}
	}
}